﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Domain.Abstract;
using Web.Domain.Entities;

namespace Web.UI.Controllers
{
    public class TypeController : Controller
    {
        private ITypeRepository repository;
        public TypeController(ITypeRepository repo)
        {
            repository = repo;
        }

        public ActionResult Create()
        {
            return View("Editor", new TypeNav());
        }
        //列表
        public ActionResult Index()
        {
            return View(repository.TypeNavs);
        }
        //添加 编辑 view
        public ActionResult Editor(int Id)
        {
            TypeNav typeNav = repository.TypeNavs.FirstOrDefault(t => t.id == Id);
            return View(typeNav);
        }

        [HttpPost]
        public ActionResult Editor(TypeNav typeNav)
        {
            if (ModelState.IsValid)
            {
                if (repository.SaveType(typeNav))
                {
                    TempData["result"] = "success";
                    TempData["message"] = string.Format("{0} 保存成功", typeNav.title);
                }
                else
                {
                    TempData["result"] = "error";
                    TempData["message"] = string.Format("{0} 保存失败", typeNav.title);
                }

                return RedirectToAction("Index");
            }
            else
            {
                return View(typeNav);
            }
        }

    }
}
